package cn.kgc.tangcco.kjde1020.mall.service.impl;

import cn.kgc.tangcco.kjde1020.mall.dao.CategoryMapper;
import cn.kgc.tangcco.kjde1020.mall.pojo.Category;
import cn.kgc.tangcco.kjde1020.mall.service.ICategoryService;
import cn.kgc.tangcco.kjde1020.mall.vo.CategoryVo;
import cn.kgc.tangcco.kjde1020.mall.vo.ResponseVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import static cn.kgc.tangcco.kjde1020.mall.consts.MallConst.ROOT_PARENT_ID;

/**
 * @ClassName CategorySericeImpl
 * @Description: TODO
 * @Author 郭建宾
 * @Date 2020/2/28
 * @Version V1.0
 **/
@Service
public class CategorySericeImpl implements ICategoryService {
    @Autowired
    private CategoryMapper categoryMapper;

    @Override
    public ResponseVo<List<CategoryVo>> selectAll() {
//        商品分类之类目查询功能
      List<Category> categories = categoryMapper.selectAll();
        //查出parent_id=0
//        for (Category category : categories) {
//            // 判断id
//            if(category.getParentId().equals(ROOT_PARENT_ID)){
//                CategoryVo categoryVo = new CategoryVo();
//                // 拷贝数据
//                BeanUtils.copyProperties(category,categoryVo);
//               categoryVoList.add(categoryVo) ;

        // Lambda + stream
        List<CategoryVo> categoryVoList = categories.stream()
                .filter(e -> e.getParentId().equals(ROOT_PARENT_ID))
                .map(this::category2CategoryVo)
                //  把SortOrder排序   reversed方法 ：反转
                .sorted(Comparator.comparing(CategoryVo::getSortOrder).reversed())
                .collect(Collectors.toList());
        // 查询子目录 对应 findSubCategory
        findSubCategory(categoryVoList, categories);
        //返回数据
        return ResponseVo.success(categoryVoList);
    }
    private  void  findSubCategory(List<CategoryVo> categoryVoList, List<Category> categories){
        //遍历一级目录
        for (CategoryVo categoryVo : categoryVoList) {
            List<CategoryVo> subCategoryVoList = new ArrayList<>();
            //遍历查询子目录
            for(Category category : categories){
                //查到内容，设置 subCategory，继续往下查
                if (categoryVo.getId().equals(category.getParentId())) {
                    CategoryVo subCategoryVo = category2CategoryVo(category);
                    subCategoryVoList.add(subCategoryVo);
                }
                // 排序 根据 SortOrder排序     方法为: 降序
                // .reversed() 反转
                subCategoryVoList.sort(Comparator.comparing(CategoryVo::getSortOrder).reversed());
                // 找到之后设置进来
                categoryVo.setSubCategories(subCategoryVoList);
                findSubCategory(subCategoryVoList, categories);
            }
        }
    }

//    商品类目之子类目
    @Override
    public void findSubCategoryId(Integer id, Set<Integer> resultSet) {
        List<Category> categories = categoryMapper.selectAll();
        //调用 findSubCategoryId方法   减少查询消耗时间
        findSubCategoryId(id,resultSet,categories);
    }

    private void findSubCategoryId(Integer id, Set<Integer> resultSet,List<Category> categories){
        for (Category category : categories){
            if(category.getParentId().equals(id)){
                resultSet.add(category.getId());
                findSubCategoryId(category.getId(),resultSet,categories);
            }
        }
    }
    private CategoryVo category2CategoryVo(Category category){
        CategoryVo categoryVo = new CategoryVo();
        BeanUtils.copyProperties(category,categoryVo);
        return categoryVo;
    }
}
